<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" showFooter :title="getTitle" width="100%" @ok="handleSubmit">
    <BasicForm @register="registerForm">
    </BasicForm>

  </BasicDrawer>
</template>
<script lang="ts" setup>
import {ref, computed, unref, reactive} from 'vue';
import { BasicForm, useForm, ApiSelect } from '@/components/Form';
import { BasicDrawer, useDrawerInner } from '@/components/Drawer';
import {formSchema} from './role.data';
import util from "@/utils/util";

import { useModal } from "@/components/Modal";
import { kqUserSave } from '@/api/heng/production';

import { useMessage } from "@/hooks/web/useMessage";
import { useUserStore } from "@/store/modules/user";
import {BasicTableProps} from "@/components/VxeTable";

import {Button, Descriptions, DescriptionsItem} from "ant-design-vue";
const userStore = useUserStore();

const [registerModal, { openModal }] = useModal();

const emit = defineEmits(['success', 'register']);
const isUpdate = ref(true);
const dataList = ref({});
const { createMessage } = useMessage();

const userId = ref("");
const [registerForm, { resetFields, setFieldsValue, validate, clearValidate }] = useForm({
  labelWidth: 100,
  baseColProps: { lg: 12, md: 24 },
  schemas: formSchema,
  showActionButtonGroup: false,
});


const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
  //打开弹层后的回调
  resetFields();
  setDrawerProps({ confirmLoading: false });
  // 需要在setFieldsValue之前先填充treeData，否则Tree组件可能会报key not exist警告
  // if (unref(treeData).length === 0) {
  //   treeData.value = (await getMenuList()) as any as TreeItem[];
  // }
  isUpdate.value = !!data?.isUpdate;
  userId.value = data.userId
  dataList.value=data;
  if (unref(isUpdate)) {
    setFieldsValue(data);
  } else {
   

  }
});



const getTitle = computed(() => (!unref(isUpdate) ? '新增车间考勤人员' : '编辑车间考勤人员'));

async function handleSubmit() {
  try {
    const values = await validate();
    // let t_list = tableRef.value.getTableData().tableData;
    // values.items = t_list;
    setDrawerProps({ confirmLoading: true });
    // TODO custom api
    values.userId = userId.value;
    createFn(values);

  } finally {
    setDrawerProps({ confirmLoading: false });
  }
}

function createFn(values) {
  kqUserSave(values).then((res) => {
    // console.log("---res::",res)
    if (res.code == 200) {
      createMessage.success(res.message);
    } else {
      createMessage.error(res.message);
    }
  }).catch((res) => {
      createMessage.error(res.message);
    }).finally(() => {
      closeDrawer();
      emit('success');
    });
}





</script>
